package com.cskaoyan.service.mall;

import com.cskaoyan.mapper.*;
import com.cskaoyan.model.bean.*;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.mall.IssueCreatBo;
import com.cskaoyan.model.bo.mall.KeyWordCreatBo;
import com.cskaoyan.model.vo.mall.IssueVo;
import com.cskaoyan.model.vo.mall.KeywordVo;
import com.cskaoyan.model.vo.mall.OrderDetailVo;
import com.cskaoyan.model.vo.mall.OrderVo;
import com.cskaoyan.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MallAfterOrderServiceImpl implements MallAfterOrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public OrderVo getOrderList(PageBo pageBo, String orderSn, Integer userId, List<String> orderStatusArray) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());//根据￥做sql语句拼接
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdIsNotNull()
                .andDeletedEqualTo(false);

        if (StringUtils.isNotEmpty(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (orderStatusArray != null) {
            criteria.andOrderStatusIn(orderStatusArray);
        }

        List<Order> orderList = orderMapper.selectByExample(orderExample);

        PageInfo<Order> orderPageInfo = new PageInfo<>(orderList);
        long total = orderPageInfo.getTotal();

        OrderVo orderVo = new OrderVo();
        orderVo.setItems(orderList);
        orderVo.setTotal(total);
        return orderVo;
    }

    @Override
    public OrderDetailVo orderDetail(int orderId) {

        OrderDetailVo orderDetailVo = new OrderDetailVo();

        Order order = orderMapper.selectByPrimaryKey(orderId);
        orderDetailVo.setOrder(order);

        OrderGoods orderGoods = orderGoodsMapper.selectByPrimaryKey(order.getId());
        List<OrderGoods> orderGoodsList = new ArrayList<>();
        orderGoodsList.add(orderGoods);
        orderDetailVo.setOrderGoods(orderGoodsList);

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdEqualTo(order.getUserId());
        List<User> users = userMapper.selectByExample(userExample);
        User user = new User();
        user.setNickname(users.get(0).getNickname());
        user.setAvatar(users.get(0).getAvatar());
        orderDetailVo.setUser(user);

        return orderDetailVo;
    }

    @Override
    public boolean orderRefund(Integer orderId) {
        Order order = new Order();
        order.setOrderStatus(203);

        OrderExample orderExample = getOrderExample(orderId);
        int i = orderMapper.updateByExampleSelective(order, orderExample);

        return i == 1;
    }

    private OrderExample getOrderExample(Integer orderId) {
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(orderId);
        return orderExample;
    }

    @Override
    public boolean orderShip(Integer orderId, String shipChannel, String shipSn) {

        Order order = new Order();
        order.setOrderStatus(301);
        order.setShipChannel(shipChannel);
        order.setShipSn(shipSn);

        OrderExample orderExample = getOrderExample(orderId);
        int i = orderMapper.updateByExampleSelective(order, orderExample);

        return i == 1;

    }
    //Issue接口

    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueVo getIssueList(PageBo pageBo, String question) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andIdIsNotNull()
                .andDeletedEqualTo(false);

        if (StringUtils.isNotEmpty(question)) {
            criteria.andQuestionLike("%" + question + "%");
        }

        List<Issue> issues = issueMapper.selectByExample(issueExample);

        PageInfo<Issue> issuePageInfo = new PageInfo<>(issues);
        long total = issuePageInfo.getTotal();

        IssueVo issueVo = new IssueVo();
        issueVo.setItems(issues);
        issueVo.setTotal(total);
        return issueVo;
    }

    @Override
    public Issue issueCreat(IssueCreatBo issueCreatBo) {

        Issue issue = new Issue();
        issue.setAnswer(issueCreatBo.getAnswer());
        issue.setQuestion(issueCreatBo.getQuestion());
        int i = issueMapper.insertSelective(issue);
        Issue creatIssue = issueMapper.selectByPrimaryKey(issue.getId());

        return creatIssue;
    }

    @Override
    public int issueDelete(Integer id) {

        Issue issue = new Issue();
        issue.setId(id);
        issue.setDeleted(true);

        int i = issueMapper.updateByPrimaryKeySelective(issue);
        if (i == 1) {
            return 1;
        }
        return -1;
    }

    @Override
    public int issueUpdate(Issue issue) {

        int i = issueMapper.updateByPrimaryKeySelective(issue);
        if (i == 1) {
            return 1;
        }
        return -1;

    }

    //Keyword

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public KeywordVo getKeyWordList(PageBo pageBo, String keyword, String url) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIdIsNotNull()
                .andDeletedEqualTo(false);
        if (StringUtils.isNotEmpty(keyword)) {

            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (StringUtils.isNotEmpty(url)) {

            criteria.andUrlLike("%" + url + "%");
        }
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);

        KeywordVo keywordVo = new KeywordVo();
        keywordVo.setItems(keywords);

        PageInfo<Keyword> keywordPageInfo = new PageInfo<>(keywords);
        long total = keywordPageInfo.getTotal();

        keywordVo.setTotal(total);

        return keywordVo;
    }

    @Override
    public Keyword keywordCreat(KeyWordCreatBo keyWordCreatBo) {

        Keyword keyword = new Keyword();
        keyword.setIsDefault(keyWordCreatBo.isDefault());
        keyword.setIsHot(keyWordCreatBo.isHot());
        keyword.setUrl(keyWordCreatBo.getUrl());
        keyword.setKeyword(keyWordCreatBo.getKeyword());
        int i = keywordMapper.insertSelective(keyword);
        Keyword keyword1 = keywordMapper.selectByPrimaryKey(keyword.getId());
        if (i == 1) {
            return keyword1;
        }
        return null;

    }

    @Override
    public int keywordDelete(Integer id) {
        Keyword keyword = new Keyword();
        keyword.setId(id);
        keyword.setDeleted(true);
        int i = keywordMapper.updateByPrimaryKeySelective(keyword);
        if (i == 1) {
            return i;
        }
        return -1;
    }

    @Override
    public int keywordUpdate(Keyword keyword) {

        int i = keywordMapper.updateByPrimaryKeySelective(keyword);
        if (i == 1) {
            return i;
        }
        return -1;
    }
}
